
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#include "GRTGeoCylinder.h"
#include <math.h>
#include "GRTGLVisu.h"
#include "RTMatrix44.h"
//#include <QGLWidget>
#include <QtOpenGL\qgl.h>
#include "GRTMemoryLeak.h"
//constructor
GRTGeoCylinder::GRTGeoCylinder() : GRTGeometry(GT_CYLINDER), _radius(1), _height(2)
{
	_rotationMarkRadius = _height * 1.5f;
	//_translateMark = RTVector3f(0, _height + 0.5f, 0);
	_translateMarkOffset = 0.5;
	_translateMark = RTVector3f(0, 0, 0);
	_translateMark.y = _height + _translateMarkOffset;
}

//destructor
GRTGeoCylinder::~GRTGeoCylinder(){}

//determine if the point v is a point of the geometry object
bool GRTGeoCylinder::isGRTGeometry(const RTVector3f& v)const
{
	//calculate the world matrix of the primitive
	RTMatrix44 t;
	t.createTranslation(_translate);

	RTMatrix44 rX;
	rX.createRotationX(_rotate.x * 3.14f / 180.f);
	RTMatrix44 rY;
	rY.createRotationY(_rotate.y * 3.14f / 180.f);
	RTMatrix44 rZ;
	rZ.createRotationZ(_rotate.z * 3.14f / 180.f);

	RTMatrix44 s;
	s.createScale(_scale);

	RTMatrix44 transform = s * rZ * rY * rX * t;

	//get V in local coordinate of the sphere
	RTVector3f localV = v * transform.inverse();

	//check the point is in the cylinder's body
	const float offsetError = 0.1f;
	const float radiusError = 0.4f;
	float maxRadius = _radius + radiusError;
	float minRadius = _radius - radiusError;
	float n = RTVector3f(localV.x, 0, localV.z).norme();
	if(localV.y > offsetError && localV.y < _height - offsetError && n > minRadius && n < maxRadius)
		return true;

	//check the top
	if(localV.y < _height + offsetError && localV.y > _height - offsetError && n < maxRadius)
		return true;

	//check bottom
	if(localV.y < offsetError && localV.y >  -offsetError && n < maxRadius)
		return true;

	//no geometry
	return false;
}

//set the sphere radius
void GRTGeoCylinder::setDimensions(float radius, float height)
{
	_radius = radius;
	_height = height;

	_rotationMarkRadius = _radius < _height ? _height : _radius;
	_rotationMarkRadius = _rotationMarkRadius * 1.5;
	_translateMark.y = _height + _translateMarkOffset;
}

//radius getter
float GRTGeoCylinder::getRadius()const
{
	return _radius;
}

//height getter
float GRTGeoCylinder::getHeight()const
{
	return _height;
}

//render the object
void GRTGeoCylinder::Render()
{
	VISU.drawCylinder(_radius, _height);
}

//post render function
void GRTGeoCylinder::postRender()
{
	//if the sphere is selected, draw the surrounding box and the translation mark
	if(_selected)
	{
		glTranslatef(0, _height / 2.f, 0);
		VISU.drawSelectionBox(_radius * 2, _height+1, _radius * 2);
	}

	glPopMatrix();
}